package com.lxl.bt.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.lxl.bt.R;
import com.lxl.bt.adapter.MyAgentAdapter;
import com.lxl.bt.bean.AgentNumberBean;
import com.lxl.bt.net.NetApi;
import com.lxl.bt.net.Result;
import com.lxl.bt.retorfit.NetCallback;
import com.lxl.bt.retorfit.RetrofitUtil;
import com.lxl.bt.util.ToastUtil;
import com.lxl.bt.view.CustomViewPager;
import com.yanzhenjie.sofia.Sofia;

import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyAgentActivity extends BaseActivity {

    @BindView(R.id.rlBack)
    RelativeLayout rlBack;
    @BindView(R.id.txt_toolbar_title)
    TextView txtToolbarTitle;
    @BindView(R.id.tv_agent_number)
    TextView tvAgentNumber;
    @BindView(R.id.tv_today_new_add)
    TextView tvTodayNewAdd;
    @BindView(R.id.tv_yesterday_new_add)
    TextView tvYesterdayNewAdd;
    @BindView(R.id.tv_week_new_add)
    TextView tvWeekNewAdd;

    @BindView(R.id.tab_layout)
    SlidingTabLayout tabLayout;
    @BindView(R.id.view_pager)
    CustomViewPager viewPager;

    private MyAgentAdapter myAgentAdapter;
    private String[] titles = {"全部","今日","昨天","近7天","近30天"};

    public static void intent(Context fromContext){
        Intent intent = new Intent(fromContext, MyAgentActivity.class);
        fromContext.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_agent);

        ButterKnife.bind(this);

        Sofia.with(this).invasionStatusBar()
                .statusBarLightFont()
                .statusBarBackgroundAlpha(0)
                .invasionNavigationBar()
                .navigationBarBackgroundAlpha(0);

        rlBack.setVisibility(View.VISIBLE);
        txtToolbarTitle.setText(getResources().getString(R.string.wk_my_agent));

        initViewPager();

        //获取代理人数
        getAgentNumber();
    }

    private void initViewPager() {
        myAgentAdapter = new MyAgentAdapter(getSupportFragmentManager(),titles);
        viewPager.setAdapter(myAgentAdapter);
        tabLayout.setViewPager(viewPager);
        viewPager.setCurrentItem(0);
        viewPager.setOffscreenPageLimit(4);
        refTableTitleView(0);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                refTableTitleView(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void refTableTitleView(int position) {
        for (int i = 0; i < titles.length; i++) {
            if (i == position) {
                tabLayout.getTitleView(position).getPaint().setFakeBoldText(true);
            } else {
                tabLayout.getTitleView(i).getPaint().setFakeBoldText(false);
            }
        }
    }

    @OnClick({R.id.rlBack})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rlBack:
                finish();
                break;
        }
    }

    private void getAgentNumber(){
        RetrofitUtil.getInstance().create(NetApi.class, 0).agentNumber()
                .enqueue(new NetCallback<Result<AgentNumberBean>>() {
                    @Override
                    public void onSuccess(Result<AgentNumberBean> result) {
                        if (result.isSuccess()) {
                            AgentNumberBean data = result.getData();
                            tvAgentNumber.setText(data.totalPers);
                            tvTodayNewAdd.setText(data.increNumPers);
                            tvYesterdayNewAdd.setText(data.yesNumPers);
                            tvWeekNewAdd.setText(data.lastSevenPers);
                        }else{
                            ToastUtil.showToast(MyAgentActivity.this,result.getMsg());
                        }
                    }

                    @Override
                    public void netLogin() {

                    }

                    @Override
                    public void onError(String errorStr) {
                        super.onError(errorStr);
                        Log.w("lxl", errorStr);
                    }
                });
    }

}










